package com.coin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.SysRole;
import com.coin.service.SysRoleService;
import com.coin.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * @author 李力
 * @description 针对表【sys_role(角色)】的数据库操作Service实现
 * @createDate 2025-05-24 17:08:03
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    /**
     * @Description: 根据用户id判断用户是否是超级管理员
     * @Author: 李 力
     * @Date: 2025/5/26
     * @Param: userId
     * @return: boolean
     */
    @Override
    public boolean checkSuperAdmin(Long userId) {
        if (userId == null) {
            throw new RuntimeException("用户参数为空");
        }
        String userCode = roleMapper.getUserCodeById(userId);
        return userCode != null && userCode.equals("ROLE_ADMIN");
    }


    /**
     * @Description: 角色分页查询
     * @Author: 李 力
     * @Date: 2025/5/27
     * @Param: current  size   name
     * @return: R
     */
    @Override
    public Page<SysRole> findByPage(Page<SysRole> page, String name) {
        return page(page, new LambdaQueryWrapper<SysRole>().like(StrUtil.isNotEmpty(name), SysRole::getName, name));
    }

    /**
     * @Description: 删除角色
     * @Author: 李 力
     * @Date: 2025/5/27
     * @Param: ids
     * @return: boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRoleByIds(String[] ids) {
        if (ids == null || ids.length == 0) {
            throw new RuntimeException("参数为空");
        }
        return removeByIds(Arrays.asList(ids));
    }
}




